package com.jade.store.wechat.biz.auth;

import com.jade.store.wechat.biz.business.Jssdk.GetJsTicketBusiness;
import com.jade.store.wechat.biz.common.Signature;
import com.jade.store.wechat.biz.protocol.Jssdk.GetJsTicketReq;
import com.jade.store.wechat.biz.protocol.Jssdk.GetJsTicketRes;
import com.jade.zsy.base.common.RandomStringGenerator;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: jinya
 * Date: 2017/4/13
 * Time: 16:00
 * TEL :17701640710   MAIL:2009_jinya@163.com
 * To change this template use File | Settings | File Templates.
 */
public class JsWxConfig {
    /**
     * 公众号编号
     */
    private String appId;
    /**
     * 时间戳
     */
    private String timestamp;
    /**
     * 生成签名的随机串
     */
    private String nonceStr;
    /**
     * 必填，签名
     */
    private String signature;

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getNonceStr() {
        return nonceStr;
    }

    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
    }

    public  JsWxConfig(String _appid,String _url){
        this.appId=_appid;
        this.nonceStr= RandomStringGenerator.getRandomStringByLength(32);
        this.timestamp=String.valueOf(System.currentTimeMillis()/1000);
        try{
            GetJsTicketBusiness _business=new GetJsTicketBusiness(_appid);
            GetJsTicketRes _res=_business.run(new GetJsTicketReq());
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("noncestr",nonceStr);
            map.put("jsapi_ticket",_res.getTicket());
            map.put("timestamp",timestamp);
            map.put("url",_url);
            String sign = Signature.getJsSign(map);
            this.signature=sign;
        }catch (Exception e){

        }

    }
}
